package cn.org.timespace.easyframe.util.sms.telecom;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.util.sms.telecom.smsUtil.SMGPPropertie;

import com.huawei.insa2.util.Args;

public class SendReceiveInterface extends Thread {
	
	private final static Log log = LogFactory.getLog(SendReceiveInterface.class);
	
	// ��¼����
	private static final String host = SMGPPropertie.getProperty("smgp.host")
			.trim();// l�ӵ�ַ
	private static final int port = Integer.parseInt(SMGPPropertie.getProperty(
			"smgp.port").trim());// l�Ӷ˿�
	private static final String clientid = SMGPPropertie.getProperty(
			"smgp.clientid").trim();// �ʺ�
	private static final String sharedSecret = SMGPPropertie.getProperty(
			"smgp.sharedSecret").trim();// ����
	private static final String transactionTimeout = SMGPPropertie.getProperty(
			"smgp.transactionTimeout").trim();// ����ʱʱ��(��λ����)
	private static final String heartbeatInterval = SMGPPropertie.getProperty(
			"smgp.heartbeatInterval").trim();// #������Ϣ���ͼ��ʱ��(��λ����)
	private static final String reconnectInterval = SMGPPropertie.getProperty(
			"smgp.reconnectInterval").trim();// l���ж�ʱ��l���ʱ��(��λ����)
	private static final String heartbeatNoresponseout = SMGPPropertie
			.getProperty("smgp.heartbeatNoresponseout").trim();// ��Ҫ��lʱ��l��������û�н��յ���Ӧ�ĸ���λ����)
	private static final String version = SMGPPropertie.getProperty(
			"smgp.version").trim();
	private static final String debug = SMGPPropertie.getProperty("smgp.debug")
			.trim();

	/*
	 * // ���ŷ��Ͳ��� private static final String spid = SMGPPropertie
	 * .getProperty("smgp.srcTermId");// ��ҵ���� private static final String spnum
	 * = SMGPPropertie .getProperty("smgp.chargeTermId");// �ƷѺ���
	 */
	private static Args arg;

	private static MySMGPSMProxy myProxy;

	public static int proxyStatus = 0;

	private SendReceiveInterface() {
	}

	private static SendReceiveInterface single;

	// ֻʵ��һ��
	static {
		single = new SendReceiveInterface();

		if (myProxy == null) {
			try {
				arg = new Args();
				arg.set("host", host);
				arg.set("port", port);
				arg.set("clientid", clientid);
				arg.set("shared-secret", sharedSecret);
				arg.set("transaction-timeout", transactionTimeout);
				arg.set("heartbeat-interval", heartbeatInterval);
				arg.set("heartbeat-noresponseout", heartbeatNoresponseout);
				arg.set("reconnect-interval", reconnectInterval);
				arg.set("version", version);
				arg.set("debug", debug);

				myProxy = new MySMGPSMProxy(new DeliverMsg(), arg);
				proxyStatus = 0;
				log.info("login status:" + myProxy.getConnState());
			} catch (Exception e) {
				proxyStatus = -4;
				log.error("l�ӵ������ʧ��:"+e.getMessage());
			}

		}
	}

	// ��̬��������
	public synchronized static SendReceiveInterface getInstance() {
		if (single == null) {
			single = new SendReceiveInterface();
		}
		return single;
	}

	/**
	 * ���Ͷ���
	 * 
	 * @param String
	 *            [] mobile notNull ���ŷ���Ŀ�����
	 * @param String
	 *            content notNull ��������
	 * */
	public boolean Send(String[] mobile, String content) {
		int mobileLen = mobile.length;

		boolean bool = false;
		boolean bool1 = false;
		if (mobileLen > 50) {
			if (mobileLen % 50 != 0) {
				int len = mobileLen / 50 + 1;
				for (int i = 0; i < len; i++) {// Ⱥ������ÿ��50��������
					String[] phone = { "" };
					for (int j = i * 50; j < (i + 1) * 50; j++) {// ȡ50�����
						if (j == mobileLen)
							break;
						else
							phone[j % 50] = mobile[j];
					}
					int contentLen = content.length();
					if (contentLen > 70) {
						if (contentLen % 70 != 0) {
							int contlen = contentLen / 70 + 1;
							for (int n = 0; n < contlen; n++) {
								String sendContent = "";
								if (n != contlen - 1)
									sendContent = content.substring(n * 70,
											(n + 1) * 70);
								else
									sendContent = content.substring(n * 70,
											contentLen);

								bool1 = SendMsg
										.send(myProxy, phone, sendContent );
								if(bool1)
									bool = true;
							}
						} else {
							int contlen = contentLen / 70;
							for (int n = 0; n < contlen; n++) {
								String sendContent = content.substring(n * 70,
										(n + 1) * 70);
								bool1 = SendMsg
										.send(myProxy, phone, sendContent );
								if(bool1)
									bool = true;
							}
						}
					} else {
						bool1 = SendMsg.send(myProxy, phone, content );
						if(bool1)
							bool = true;
					}
				}
			} else {
				int len = mobileLen / 50;
				for (int i = 0; i < len; i++) {// Ⱥ������ÿ��50��������
					String[] phone = { "" };
					for (int j = i * 50; j < (i + 1) * 50; j++) {// ȡ50�����
						if (j == mobileLen)
							break;
						else
							phone[j % 50] = mobile[j];
					}

					int contentLen = content.length();
					if (contentLen > 70) {
						if (contentLen % 70 != 0) {
							int contlen = contentLen / 70 + 1;
							for (int n = 0; n < contlen; n++) {
								String sendContent = "";
								if (n != contlen - 1)
									sendContent = content.substring(n * 70,
											(n + 1) * 70);
								else
									sendContent = content.substring(n * 70,
											contentLen);

								bool1 = SendMsg
										.send(myProxy, phone, sendContent );
								if(bool1)
									bool = true;
							}
						} else {//��������70���� 
							int contlen = contentLen / 70;
							for (int n = 0; n < contlen; n++) {
								String sendContent = content.substring(n * 70,
										(n + 1) * 70);
								bool1 = SendMsg
										.send(myProxy, phone, sendContent );
								if(bool1)
									bool = true;
							}
						}
					} else {
						bool1 = SendMsg.send(myProxy, phone, content );
						if(bool1)
							bool = true;
					}
				}
			}
		} else {//����50��绰����
			int contentLen = content.length();
			if (contentLen > 70) {
				if (contentLen % 70 != 0) {
					int contlen = contentLen / 70 + 1;
					for (int n = 0; n < contlen; n++) {
						String sendContent = "";
						if (n != contlen - 1)
							sendContent = content.substring(n * 70,
									(n + 1) * 70);
						else
							sendContent = content.substring(n * 70, contentLen);

						bool1 = SendMsg.send(myProxy, mobile, sendContent );
						if(bool1)
							bool = true;
					}
				} else {
					int contlen = contentLen / 70;
					for (int n = 0; n < contlen; n++) {
						String sendContent = content.substring(n * 70,
								(n + 1) * 70);
						bool1 = SendMsg.send(myProxy, mobile, sendContent );
						if(bool1)
							bool = true;
					}
				}
			} else {//��������70����
				bool1 = SendMsg.send(myProxy, mobile, content );
				if(bool1)
					bool = true;
			}
		}
		return bool;
	}

	/**
	 * ����ն��Ź���
	 * */
	public void ReceiveInstance() {
		log.info("����ն��Ź���");
		new Thread() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(1000 * 60 * 10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
}
